package palmtop.com.palmtop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.beardedhen.androidbootstrap.AwesomeTextView;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.util.ArrayList;

import palmtop.com.palmtop.R;
import palmtop.com.palmtop.activity.ProcessdetailActivity;
import palmtop.com.palmtop.activity.ProcesstaskActivity;
import palmtop.com.palmtop.model.Task;
import palmtop.com.palmtop.model.Taskgrid;
import palmtop.com.palmtop.util.BadgeView;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.FuncUtil;
import palmtop.com.palmtop.util.NetUtil;

/**
 * Created by Administrator on 2017/9/1.
 */

public class TaskgridAdapter extends BaseAdapter {
    private ArrayList<Taskgrid> arrlist;
    private Context context;
    String role_id;

    public TaskgridAdapter(ArrayList<Taskgrid> arrlist, Context context) {
        this.arrlist = arrlist;
        this.context = context;
        AppPreferences appPreferences = new AppPreferences(context);
        role_id = appPreferences.getString("role_id","");
    }

    @Override
    public int getCount() {
        return arrlist.size();
    }

    @Override
    public Object getItem(int i) {
        return arrlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int pos, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.item_taskgrid,null);
        AwesomeTextView unitname = view.findViewById(R.id.unitname);
        AwesomeTextView status = view.findViewById(R.id.status);
        AwesomeTextView adddate = view.findViewById(R.id.adddate);
        AwesomeTextView taskname1 = view.findViewById(R.id.taskname1);
        Button xiangqing = view.findViewById(R.id.xiangqing);



        final Taskgrid Taskgrid = arrlist.get(pos);
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            selectTaskread(Taskgrid,taskname1);
        }

        unitname.setText(Taskgrid.getUnitname());
        if(Taskgrid.getStatus().equals("0")){
            status.setText("未接收任务");
        }
        if(Taskgrid.getStatus().equals("1")){
            status.setText("任务已签收");
        }
        if(Taskgrid.getStatus().equals("2")){
            status.setText("任务已派发");
        }
        if(Taskgrid.getStatus().equals("4")){
            status.setText("民警已执行");
        }

        adddate.setText(Taskgrid.getAdddate());
        xiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();

                intent.putExtra("taskgrid",Taskgrid);
                /*String taskid = String.valueOf(Taskgrid.getId());
                String ssgrid = String.valueOf(Taskgrid.getUnitid());
                String puserid = String.valueOf(Taskgrid.get());*/
                if(Taskgrid.getStatus().equals("4")){
//                    FuncUtil.updateReadTask();
                    intent.setClass(context,ProcessdetailActivity.class);


                }else{
                    intent.setClass(context,ProcesstaskActivity.class);
                }

                context.startActivity(intent);
            }
        });

        return view;
    }


    private void selectTaskread(Taskgrid taskgrid,final AwesomeTextView taskname1) {
        RequestParams params = new RequestParams(Config.SERVER_URL + "readtask/getByTaskid1");
        params.addBodyParameter("taskid", String.valueOf(taskgrid.getId()));
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(!(result==null||result.equals("\"\""))){
                    BadgeView badge = new BadgeView(context,taskname1);
                    badge.setText("未读");
                    badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
                    badge.show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });

    }
}
